package jym.tan.tmdb.method;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import jym.tan.tmdb.TMDB;
import jym.tan.tmdb.browser.WebBrowser;
import jym.tan.tmdb.constant.TMDBConstants;
import jym.tan.tmdb.json.Parser;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author Tanmoy
 *
 */
public class BaseMethod {

	private TMDB tmdb;
	private String procedure;
	private String subprocedure;
	
	protected final Parser parser = Parser.getInstance();
	
	public BaseMethod(TMDB tmdb, String procedure) {
		this(tmdb, procedure, TMDBConstants.BLANK);
	}

	public BaseMethod(TMDB tmdb, String procedure, String subprocedure) {
		this.tmdb = tmdb;
		this.procedure = procedure;
		this.subprocedure = subprocedure;
	}
	
	protected URL getUrl(String query, String movieId, String language, String country, int page, boolean includeAdult) {
        StringBuilder urlString = new StringBuilder(TMDBConstants.BASE_URL);
        urlString.append(procedure);

        if (StringUtils.isNotBlank(query)) {
            urlString.append(TMDBConstants.QUESTION_MARK);
            urlString.append(TMDBConstants.PARAM_QUERY);

            try {
                urlString.append(URLEncoder.encode(query, TMDBConstants.UTF8));
            } catch (UnsupportedEncodingException ex) {
                urlString.append(query);
            }
        }

        if (StringUtils.isNotBlank(movieId)) {
            urlString.append(movieId);
        }

        urlString.append(subprocedure);

        if (StringUtils.isBlank(query)) {
            urlString.append(TMDBConstants.QUESTION_MARK);
        } else {
            urlString.append(TMDBConstants.AMPERSAND);
        }
        urlString.append(TMDBConstants.PARAM_API_KEY);
        urlString.append(tmdb.getKey());

        if (StringUtils.isNotBlank(language)) {
            urlString.append(TMDBConstants.PARAM_LANGUAGE);
            urlString.append(language);
        }

        if (StringUtils.isNotBlank(country)) {
            urlString.append(TMDBConstants.PARAM_COUNTRY);
            urlString.append(country);
        }
        
        if (page > TMDBConstants.ZERO) {
            urlString.append(TMDBConstants.PARAM_PAGE);
            urlString.append(page);
        }
        
        if(includeAdult) {
        	urlString.append(TMDBConstants.PARAM_INCLUDE_ADULT);
        	urlString.append(includeAdult);
        }
        
        try {
            return new URL(urlString.toString());
        } catch (MalformedURLException ex) {
            return null;
        }
    }	
	
	protected URL getUrl(String movieId, String language, String country) {
		return getUrl(TMDBConstants.BLANK, movieId, language, country, TMDBConstants.ZERO, Boolean.FALSE);
	}
	
	protected <T> T getTMDBObject(URL getURL, Class<T> clazz) throws Exception {
		String json = WebBrowser.request(getURL);
		return (T) parser.parseJson(json, clazz);
	}
}
